﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThouShaltNotBrickLevelEditor.Code.GeneralObjects;
using GMUtilitiesDotNet.Threading;
using ThouShaltNotBrickLevelEditor.Code.Renderers;
using GMUtilitiesDotNet.Events;

namespace ThouShaltNotBrickLevelEditor
{
    /// <summary>
    /// Interaction logic for BrickStateSelectorView.xaml
    /// </summary>
    public partial class BrickStateSelectorView : UserControl
    {
        #region Properties
        #region BrickSelectorRects
        /// <summary>
        /// BrickSelectorRects property.
        /// </summary>
        public BrickSelectorRects BrickSelectorRects { get; private set; }
        #endregion

        private CustomBackgroundWorker<BrickStateSelectorRendererParameters, BrickStateSelectorRendererReturnValue> mBrickStateSelectorRenderer;
        #endregion

        #region Dependency Properties
        #region BrickStatesTypesImages
        public static readonly DependencyProperty BrickStatesTypesImagesProperty = DependencyProperty.Register(
            "BrickStatesTypesImages",
            typeof(BrickStatesTypesImages),
            typeof(BrickStateSelectorView),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnBrickStatesTypesImagesChanged)));

        public BrickStatesTypesImages BrickStatesTypesImages
        {
            get { return (BrickStatesTypesImages)GetValue(BrickStatesTypesImagesProperty); }
            set { SetValue(BrickStatesTypesImagesProperty, value); }
        }

        public static void OnBrickStatesTypesImagesChanged(DependencyObject pObj, DependencyPropertyChangedEventArgs pArgs)
        {
            ((BrickStateSelectorView)pObj).OnBrickStatesTypesImagesChanged(pArgs);
        }

        private void OnBrickStatesTypesImagesChanged(DependencyPropertyChangedEventArgs pArgs)
        {
            Redraw();
        }
        #endregion

        #region BrickStateSelection
        public static readonly DependencyProperty BrickStateSelectionProperty =
            DependencyProperty.Register(
                "BrickStateSelection",
                typeof(BrickStateSelection),
                typeof(BrickStateSelectorView),
                new PropertyMetadata(null, OnBrickStateSelectionChanged));

        public BrickStateSelection BrickStateSelection
        {
            get { return (BrickStateSelection)GetValue(BrickStateSelectionProperty); }
            set { SetValue(BrickStateSelectionProperty, value); }
        }

        private static void OnBrickStateSelectionChanged(DependencyObject pObj, DependencyPropertyChangedEventArgs pArgs)
        {
            ((BrickStateSelectorView)pObj).OnBrickStateSelectionChanged(pArgs);
        }

        private void OnBrickStateSelectionChanged(DependencyPropertyChangedEventArgs args)
        {
            Redraw();
        }
        #endregion
        #endregion

        #region Constructors
        public BrickStateSelectorView()
        {
            InitializeComponent();

            BrickSelectorRects = null;
            mBrickStateSelectorRenderer = new CustomBackgroundWorker<BrickStateSelectorRendererParameters, BrickStateSelectorRendererReturnValue>(
                this,
                RendererFactory.CreateBrickStateSelectorRenderer());
        }
        #endregion

        #region Overriden Methods
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            Redraw();
            base.OnRenderSizeChanged(sizeInfo);
        }
        #endregion

        #region Methods
        private void Redraw()
        {
            BrickStateSelectorRendererParameters bssrp = new BrickStateSelectorRendererParameters(
                ActualWidth,
                ActualHeight,
                BrickStatesTypesImages,
                BrickStateSelection);

            mBrickStateSelectorRenderer.Work(bssrp);
        }
        #endregion

        #region Event Handlers
        private void LoadedHandler(object sender, RoutedEventArgs e)
        {
            mBrickStateSelectorRenderer.WorkComplete += BrickStateSelectorRendererWorkCompleteHandler;
            Redraw();
        }

        private void UnloadedHandler(object sender, RoutedEventArgs e)
        {
            mBrickStateSelectorRenderer.WorkComplete -= BrickStateSelectorRendererWorkCompleteHandler;
        }

        private void BrickStateSelectorRendererWorkCompleteHandler(object sender, ParameterizedEventArgs<BrickStateSelectorRendererReturnValue> e)
        {
            if (e.Parameter != null)
            {
                RenderTargetBitmap brickStateSelectorBitmap = e.Parameter.BrickStateSelectorBitmap;
                BrickStateSelectorImage.Source = brickStateSelectorBitmap;

                BrickSelectorRects = e.Parameter.BrickSelectorRects;
            }
            else
            {
                BrickStateSelectorImage.Source = null;
            }
        }
        #endregion
    }
}